package com.square.service.auth.provider.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.square.service.auth.provider.service.AuthService;
import com.square.service.common.consts.GlobalPrefix;
import com.square.service.common.entity.member.po.User;
import com.square.service.common.handler.ApiCode;
import com.square.service.common.utils.JwtUtils;
import com.square.service.redis.consumer.feign.RedisFeign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 认证服务层实现
 *
 * @author : 王苏
 * @since : 2021/4/21 10:56
 */
@Slf4j
@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private RedisFeign redisFeign;

    /**
     * 登录认证
     *
     * @param token
     * @return
     */
    @Override
    public R<?> doGetAuthenticationInfo(String token) {
        String redisKey = GlobalPrefix.PREFIX_LOGIN_TOKEN + JwtUtils.getTokenInfo(token, JwtUtils.JWT_CLAIM_USERNAME);
        if (StringUtils.isBlank(redisKey)) {
            return R.failed(ApiCode.INVALID_TOKEN);
        }
        R<?> r = redisFeign.get(redisKey);
        if (!r.ok()) {
            return r;
        }
        if (r.getData() == null) {
            return R.failed(ApiCode.INVALID_TOKEN);
        }
        String userStr = r.getData().toString();
        User user = JSONObject.parseObject(userStr, User.class);
        r = JwtUtils.validateJwt(token, user.getPassword());
        if (!r.ok()) {
            return R.failed(ApiCode.INVALID_TOKEN);
        }
        redisFeign.setExpire(redisKey, JwtUtils.JWT_EXPIRE_TIME);
        return R.ok(redisKey);
    }

    /**
     * 角色认证
     *
     * @param userKey
     * @param value
     * @return
     */
    @Override
    public R<?> doGetAuthorizationInfoForRoles(String userKey, String[] value) {
        R<?> r = redisFeign.get(userKey);
        if (!r.ok()) {
            return r;
        }
        if (r.getData() == null) {
            return R.failed(ApiCode.INVALID_TOKEN);
        }
        String userStr = r.getData().toString();
        User user = JSONObject.parseObject(userStr, User.class);
        for(String s :value){
            if(!user.getRoles().contains(s)){
                return R.failed(ApiCode.INVALID_PERMISSION);
            }
        }
        return R.ok(null);
    }

    /**
     * 权限认证
     *
     * @param userKey
     * @param value
     * @return
     */
    @Override
    public R<?> doGetAuthorizationInfoForPermissions(String userKey, String[] value) {
        R<?> r = redisFeign.get(userKey);
        if (!r.ok()) {
            return r;
        }
        if (r.getData() == null) {
            return R.failed(ApiCode.INVALID_TOKEN);
        }
        String userStr = r.getData().toString();
        User user = JSONObject.parseObject(userStr, User.class);
        for(String s :value){
            if(!user.getPermissions().contains(s)){
                return R.failed(ApiCode.INVALID_PERMISSION);
            }
        }
        return R.ok(null);
    }


}
